package com.symaster.common.swing.element;

import com.symaster.common.swing.config.AppConfig;
import com.symaster.common.swing.entity.MessageItem;
import com.symaster.common.swing.util.ThreadPoolUtil;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.EventListener;
import java.util.HashSet;
import java.util.Set;

/**
 * @author yinmiao
 * @date 2022-01-25 11:23
 */
public class MenuMessagePanel extends JPanel {

    private JPanel root_panel;
    private JPanel instant_panel;
    private JPanel center_list_panel;
    private Integer instantPanelHeight;
    private Boolean openLogListPanel;
    private Set<SizeChangeListener> sizeChangeListeners;

    public MenuMessagePanel() {
        initPage();
    }

    public MenuMessagePanel(Integer instantPanelHeight) {
        this.instantPanelHeight = instantPanelHeight;
        initPage();
    }

    public void print(String str, int type) {
        instant_panel.removeAll();
        MessageItem messageItem = new MessageItem(str, type, 28, true, true);
        messageItem.addClickListener(() -> {
            System.out.println("------");
        });
        instant_panel.add(messageItem);
        instant_panel.updateUI();
    }

    public void println(String str) {
        print(str, 0);
    }

    public void printf(String str, Object... objs) {
        println(String.format(str, objs));
    }

    private void initPage() {
        center_list_panel.setBackground(AppConfig.getColor("main_background_color"));
        setLayout(new BorderLayout());
        add(root_panel, BorderLayout.CENTER);
        if (instantPanelHeight != null && instantPanelHeight > 0) {
            instant_panel.setPreferredSize(new Dimension(1, instantPanelHeight));
        }
    }

    private void openLogListPanel() {
        if (this.openLogListPanel != null && this.openLogListPanel) {
            return;
        }
        this.openLogListPanel = true;
    }

    private void closeLogListPanel() {
        if (this.openLogListPanel != null && !this.openLogListPanel) {
            return;
        }
        this.openLogListPanel = false;
    }

    private void sizeChangeListener(Dimension dimension) {
        if (sizeChangeListeners == null || sizeChangeListeners.size() <= 0) {
            return;
        }
        ThreadPoolUtil.execute(() -> {
            for (SizeChangeListener sizeChangeListener : sizeChangeListeners) {
                sizeChangeListener.handler(dimension);
            }
        });
    }

    public void addSizeChangeListener(SizeChangeListener listener) {
        if (sizeChangeListeners == null) {
            sizeChangeListeners = new HashSet<>();
        }
        sizeChangeListeners.add(listener);
    }

    public void removeSizeChangeListener(SizeChangeListener listener) {
        if (sizeChangeListeners == null || sizeChangeListeners.size() <= 0) {
            return;
        }
        sizeChangeListeners.remove(listener);
    }

    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        root_panel = new JPanel();
        root_panel.setLayout(new BorderLayout(0, 0));
        root_panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        instant_panel = new JPanel();
        instant_panel.setLayout(new BorderLayout(0, 0));
        instant_panel.setBackground(new Color(-15197408));
        instant_panel.setPreferredSize(new Dimension(0, 40));
        root_panel.add(instant_panel, BorderLayout.SOUTH);
        instant_panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
        center_list_panel = new JPanel();
        center_list_panel.setLayout(new BorderLayout(0, 0));
        root_panel.add(center_list_panel, BorderLayout.CENTER);
        center_list_panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), null, TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.DEFAULT_POSITION, null, null));
    }

    /** @noinspection ALL */
    public JComponent $$$getRootComponent$$$() {
        return root_panel;
    }

    public interface SizeChangeListener extends EventListener {
        void handler(Dimension dimension);
    }

}
